/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.rakev.matcher;

import ru.rakev.producer.Producer;

/**
 *
 * @author Rakev
 */
public class ProducerMatcherAdapter<F, T> implements Matcher<T> {

    private Matcher<F> formMatcher;
    private Producer<T, F> producer;
    private boolean returnIfNullProduced;

    public ProducerMatcherAdapter(Producer<T, F> producer, Matcher<F> formMatcher, boolean returnIfNullProduced) {
        this.formMatcher = formMatcher;
        this.producer = producer;
    }

    @Override
    public boolean matches(T item) {
        F producedValue = producer.produce((T) item);
        if (producedValue != null) {
            return formMatcher.matches(producedValue);
        } else {
            return returnIfNullProduced;
        }
    }
}
